/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package aptech.edu.ixj.impl;

import aptech.edu.ixj.entity.Answer;
import aptech.edu.ixj.entity.Question;
import aptech.edu.ixj.interfaces.IQADao;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import nu.xom.Attribute;
import nu.xom.Builder;
import nu.xom.Nodes;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Serializer;
import nux.xom.xquery.XQueryUtil;
import org.w3c.dom.Node;

/**
 *
 * @author ChauNTM
 */
public class QADaoImpl implements IQADao{

    public static final String QUESTION = "D:\\Project\\thanhtv00011\\Survey\\question.xml";

    public int numberManTl = 0;
    public Document doc;
    public List<Question> getListQuestion(String surveyId) {

        List<Question> questions = new ArrayList<Question>();
        Question question;
        try {
            doc = new Builder().build(new File(QUESTION));
            Nodes nodes = XQueryUtil.xquery(doc, "//document/questions/question[@id='"+surveyId+"']");
            for (int i = 0; i < nodes.size(); i++) {
                    question = new Question();
                    question.setCald(nodes.get(i).query("@id").get(0).getValue());
                    question.setContent(nodes.get(i).query("Content").get(0).getValue());
                    questions.add(question);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return questions;
    }


    public boolean save() {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(new File(QUESTION));
            Serializer ser = new Serializer(fos);
            ser.write(doc);
            ser.flush();
            return true;
        } catch (Exception ex) {
            return false;
        } finally {
            try {
                fos.close();
            } catch (IOException ex) {

            }
        }
    }

    public boolean removeAnser(String answerId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean removeQuestion(String quesId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void reemove(Node nodeParen, Node nodeRemove) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean createQuestion(Question question) {

        try {
            Builder builder = new Builder();
            doc = builder.build(new File(QUESTION));
            nu.xom.Node nodes = doc.query("//document/questions/question").get(0);
            Element content = new Element("Content");
            content.appendChild(question.getContent());
            Element questionElement = new Element("question");
            Attribute id = new Attribute("id", question.getIdQuestion());
            questionElement.addAttribute(id);
            questionElement.appendChild(content);
            nodes.getParent().fastInsertChild(questionElement, nodes.getParent().getChildCount());
            save();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public boolean createAnswer(Answer answer) {
        try {
            Builder builder = new Builder();
            doc = builder.build(new File(QUESTION));
            nu.xom.Node nodes = doc.query("//document/answers/answer").get(0);
            Element option = new Element("option");
            option.appendChild(answer.getOption());
            Element QuesID = new Element("QuesID");
            QuesID.appendChild(answer.getQuestionId());
            Element rate = new Element("rate");
            rate.appendChild("0");
            Element answerElement = new Element("answer");
            Attribute id = new Attribute("id", answer.getIdAnswer());
            answerElement.addAttribute(id);
            answerElement.appendChild(option);
            answerElement.appendChild(QuesID);
            answerElement.appendChild(rate);
            nodes.getParent().fastInsertChild(answerElement, nodes.getParent().getChildCount());
            save();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public List<Answer> getListAnswer(String quesId) {
        List<Answer> answers = new ArrayList<Answer>();
        numberManTl = 0;
        Answer answer;
        try {
            doc = new Builder().build(new File(QUESTION));
            Nodes nodes = XQueryUtil.xquery(doc, "//document/answers/answer[QuesID='" + quesId + "']");
            for (int i = 0; i < nodes.size(); i++) {
                answer = new Answer();
                answer.setIdAnswer(nodes.get(i).query("@id").get(0).getValue());
                answer.setOption(nodes.get(i).query("option").get(0).getValue());
                answer.setQuestionId(nodes.get(i).query("QuesID").get(0).getValue());
                answer.setRating(nodes.get(i).query("rate").get(0).getValue());
                numberManTl += Integer.parseInt(answer.getRating());
                answers.add(answer);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return answers;
    }

    public boolean removeAnswer(String answerId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void remove(Node nodeParen, Node noderemove) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
